// AWTComponent.cpp: implementation of the AWTComponent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWT.h"
#include "AWTComponent.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTComponent, AWTObject)

AWTComponent::AWTComponent()
{
	m_visible = TRUE;
	m_enabled = TRUE;
	m_valid = FALSE;
	m_peer = NULL;
	m_parent = NULL;
	m_minSize = CSize(0,0);
	m_prefSize = CSize(0,0);
	m_foregnd = NULL;
	m_backgnd = NULL;
	m_brush = NULL;
	m_componentListener = NULL;
	m_focusListener = NULL;
	m_keyListener = NULL;
	m_mouseListener = NULL;
	m_mouseMotionListener = NULL;
	m_x = m_y = m_w = m_h = 0;
	m_focus = FALSE;
#ifndef UNDER_CE
	m_cursor = ::LoadCursor(NULL, IDC_ARROW);
	m_font = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
#endif
}

AWTComponent::~AWTComponent()
{
	if (m_peer != NULL)
		delete m_peer;
	if (m_foregnd != NULL)
		delete m_foregnd;
	if (m_backgnd != NULL)
		delete m_backgnd;
	if (m_brush != NULL)
		delete m_brush;

	if (m_componentListener != NULL)
	{
		if (m_componentListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_componentListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_componentListener);
#endif
			delete mc;
		}
	}
	if (m_focusListener != NULL)
	{
		if (m_focusListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_focusListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_focusListener);
#endif
			delete mc;
		}
	}
	if (m_keyListener != NULL)
	{
		if (m_keyListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_keyListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_keyListener);
#endif
			delete mc;
		}
	}
	if (m_mouseListener != NULL)
	{
		if (m_mouseListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_mouseListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_mouseListener);
#endif
			delete mc;
		}
	}
	if (m_mouseMotionListener != NULL)
	{
		if (m_mouseMotionListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_mouseMotionListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_mouseMotionListener);
#endif
			delete mc;
		}
	}
}

BOOL AWTComponent::isShowing()
{
	if (m_visible && m_peer != NULL)
	{
		AWTComponent* parent = m_parent;
		return (parent == NULL) || parent->isShowing();
	}
	return FALSE;
}

void AWTComponent::setEnabled(BOOL b)
{
	m_enabled = b;
	if (m_peer != NULL)
	{
		m_peer->EnableWindow(b);
	}
}

void AWTComponent::setVisible(BOOL b)
{
	m_visible = b;
	if (m_peer != NULL)
	{
		if (b)
			m_peer->ShowWindow(SW_SHOW);
		else
			m_peer->ShowWindow(SW_HIDE);
	}
}

void AWTComponent::reshape(int x, int y, int w, int h)
{
	
	//jd 10/11/99
	BOOL resized = (m_w != w) || (m_h != h);
  BOOL moved = (m_x != x) || (m_y != y);

	if (resized || moved) 
	{
		// Remember the area this component occupied in its parent.
		int oldParentX = m_x;
		int oldParentY = m_y;
		if (m_parent != NULL) 
		{
				CPoint pt = m_parent->getLocation();
				oldParentX += pt.x;
				oldParentY += pt.y;
		}
		int oldWidth = m_w;
		int oldHeight = m_h;
		m_x = x;
		m_y = y;
		m_w = w;
		m_h = h;
		if (m_peer != NULL) 
		{
			m_peer->MoveWindow(m_x,m_y,m_w,m_h);

			if (resized) 
			{
				invalidate();
				if (m_componentListener != NULL) 
				{
						AWTComponentEvent e(this, AWTComponentEvent::COMPONENT_RESIZED);
						processComponentEvent(&e);
				}
			}
      if (moved && (m_componentListener != NULL)) 
      {
          AWTComponentEvent e(this, AWTComponentEvent::COMPONENT_MOVED);
					processComponentEvent(&e);
      }
		  if (m_parent != NULL && m_parent->isValid()) 
		  {
		      m_parent->invalidate();
		  }
		}
	}

/*
	m_x = x;
	m_y = y;
	m_w = w;
	m_h = h;
	if (m_peer != NULL)
	{
		m_peer->MoveWindow(m_x,m_y,m_w,m_h);
	}
*/
}

void AWTComponent::invalidate()
{
	m_valid = FALSE;
	m_prefSize = CSize(0,0);
	m_minSize = CSize(0,0);
	if (m_parent != NULL)
		m_parent->invalidate();
}

void AWTComponent::addNotify()
{
	if (m_peer == NULL)
		m_peer = createComponent();
	invalidate();
}

void AWTComponent::removeNotify()
{
	if (m_peer != NULL)
	{	
		m_peer->ShowWindow(SW_HIDE);
		delete m_peer;
		m_peer = NULL;
	}
	invalidate();
}

AWTColor* AWTComponent::getBackground() 
{
	AWTColor* background = m_backgnd;
	if (background != NULL) 
	{
		return background;
	}
  AWTContainer* parent = (AWTContainer*)m_parent;
	return (m_parent != NULL) ? m_parent->getBackground() : NULL;
}

//16/2/00 jd
AWTColor* AWTComponent::getForeground() 
{
	AWTColor* foreground = m_foregnd;
	if (foreground != NULL) 
	{
		return foreground;
	}
  AWTContainer* parent = (AWTContainer*)m_parent;
	return (m_parent != NULL) ? m_parent->getForeground() : NULL;
}

CBrush* AWTComponent::getBkBrush()
{
	if (m_brush != NULL)
		return m_brush;
  AWTContainer* parent = (AWTContainer*)m_parent;
	return (m_parent != NULL) ? m_parent->getBkBrush() : NULL;
}

void AWTComponent::setBackground(AWTColor* c) 
{
	if (m_backgnd != NULL)
		delete m_backgnd;
	if (m_brush != NULL)
	{
		delete m_brush;
		m_brush = NULL;
	}
	m_backgnd = c;
	if (m_backgnd != NULL)
	{
		m_brush = new CBrush();
		m_brush->CreateSolidBrush(m_backgnd->getColorRef());
	}
}

void AWTComponent::processComponentEvent(AWTComponentEvent* e) 
{
	if (m_componentListener != NULL) 
	{
    int id = e->getId();
    switch(id) 
		{
      case AWTComponentEvent::COMPONENT_RESIZED:
        m_componentListener->componentResized(e);
        break;
      case AWTComponentEvent::COMPONENT_MOVED:
        m_componentListener->componentMoved(e);
        break;
      case AWTComponentEvent::COMPONENT_SHOWN:
        m_componentListener->componentShown(e);
        break;
      case AWTComponentEvent::COMPONENT_HIDDEN:
        m_componentListener->componentHidden(e);
        break;
		}
  }
}

void AWTComponent::processFocusEvent(AWTFocusEvent* e) 
{
	if (m_focusListener != NULL) 
	{
    int id = e->getId();
    switch(id) 
		{
      case AWTFocusEvent::FOCUS_GAINED:
        m_focusListener->focusGained(e);
        break;
      case AWTFocusEvent::FOCUS_LOST:
        m_focusListener->focusLost(e);
        break;
		}
  }
}

void AWTComponent::processKeyEvent(AWTKeyEvent* e) 
{
	if (m_keyListener != NULL) 
	{
    int id = e->getId();
    switch(id) 
		{
      case AWTKeyEvent::KEY_TYPED:
        m_keyListener->keyTyped(e);
        break;
      case AWTKeyEvent::KEY_PRESSED:
        m_keyListener->keyPressed(e);
        break;
      case AWTKeyEvent::KEY_RELEASED:
        m_keyListener->keyReleased(e);
        break;
		}
  }
}

void AWTComponent::processMouseEvent(AWTMouseEvent* e) 
{
	if (m_mouseListener != NULL) 
	{
    int id = e->getId();
    switch(id) 
		{
      case AWTMouseEvent::MOUSE_CLICKED:
        m_mouseListener->mouseClicked(e);
        break;
      case AWTMouseEvent::MOUSE_PRESSED:
        m_mouseListener->mousePressed(e);
        break;
      case AWTMouseEvent::MOUSE_RELEASED:
        m_mouseListener->mouseReleased(e);
        break;
      case AWTMouseEvent::MOUSE_ENTERED:
        m_mouseListener->mouseEntered(e);
        break;
      case AWTMouseEvent::MOUSE_EXITED:
        m_mouseListener->mouseExited(e);
        break;
		}
  }
	if (m_mouseMotionListener != NULL) 
	{
    int id = e->getId();
    switch(id) 
		{
      case AWTMouseEvent::MOUSE_MOVED_EVENT:
        m_mouseMotionListener->mouseMoved(e);
        break;
      case AWTMouseEvent::MOUSE_DRAGGED:
        m_mouseMotionListener->mouseDragged(e);
        break;
		}
  }
}

void AWTComponent::eraseBackground(CDC* pDC)
{
	if (m_peer != NULL)
	{
		CBrush* br = getBkBrush();
		if (br != NULL)
		{
			CBrush* brOld = pDC->SelectObject(br);
			CRect rc;
			m_peer->GetClientRect(&rc);
			pDC->FillRect(&rc, br);
			pDC->SelectObject(brOld);
		}
	}
}

void AWTComponent::requestFocus()
{
	if (m_peer != NULL) 
	{	
		AWTComponent* comp = this;
		do {
			comp = comp->getParent();
			if (comp != NULL)
			{
				if (comp->IsKindOf(RUNTIME_CLASS(AWTApplet)))
				{
					AWTFocusManager* fm = ((AWTApplet*)comp)->getFocusManager();	
					if (fm != NULL)
						fm->setFocusOwner(this);
					break;
				}
				else if (comp->IsKindOf(RUNTIME_CLASS(AWTWindow)))
				{
					AWTFocusManager* fm = ((AWTWindow*)comp)->getFocusManager();	
//					if (fm != NULL)
//						fm->setFocusOwner(this);
					comp->getPeer()->SetForegroundWindow();
					break;
				}
			}
		} while (comp != NULL);
		m_peer->SetFocus();
	}	 

	m_focus = TRUE;
}

void AWTComponent::setFont(HFONT f)
{
	m_font = f;
	if (m_peer != NULL)
		m_peer->SetFont(CFont::FromHandle(m_font));
}


